﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;
using System.Web.Security;
using VCC.Lib.Common;
namespace VCC.Lib.Helpers
{
    public static class MenuHelper
    {
        public static string Menu(this HtmlHelper helper, string className)
        {
            var sb = new StringBuilder();

            // Create opening unordered list tag
            sb.Append(string.Format("<ul class='{0}'>", className));

            // Render each top level node
            var topLevelNodes = SiteMap.RootNode.ChildNodes;
            
            foreach (SiteMapNode node in topLevelNodes)
            {
                sb.Append(MenuItem(helper, node));
            }

            // Close unordered list tag
            sb.Append("</ul>");
            return sb.ToString();
        }
        private static string MenuItem(this HtmlHelper helper, SiteMapNode parent)
        {
            StringBuilder sb = new StringBuilder();
            if (parent.Roles.Count <=0 || CheckRole(parent.Roles[0].ToString()))
            {
                if (SiteMap.CurrentNode == parent)
                    sb.AppendLine("<li class='current'>");
                else
                    sb.AppendLine("<li>");

                sb.AppendFormat("<a href='{0}'>{1}</a>", parent.Url, helper.Encode(parent.Title));
                if (parent.ChildNodes.Count > 0)
                {
                    sb.AppendLine("<ul>");
                    foreach (SiteMapNode node in parent.ChildNodes)
                    {
                        sb.AppendLine(MenuItem(helper, node));
                    }
                    sb.AppendLine("</ul>");
                }
                sb.AppendLine("</li>");
            }
            return sb.ToString();
        }
        private static bool CheckRole(string userName, string role)
        {
            try
            {
                VCC.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new VCC.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                MembershipUser u = a.Get(userName);
                VCC.Lib.MvcMembership.AspNetRoleProviderWrapper r = new VCC.Lib.MvcMembership.AspNetRoleProviderWrapper(Roles.Provider);
                if (r != null && u != null)
                {
                    if (r.IsInRole(u, RoleSystem.Administrator)) return true;
                    else
                        return r.IsInRole(u, role);
                }
                else return false;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        private static bool CheckRole(string role)
        {
            string userName = HttpContext.Current.User.Identity.Name;
            return CheckRole(userName, role);
        }
        private static Guid GetUserId()
        {
            MembershipUser myObject = Membership.GetUser();
            return new Guid(myObject.ProviderUserKey.ToString());
        }
    }
}